﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using YouWei.Infrastructure.Service;

namespace YouWei.Console
{
    public class BaiduLocationApi
    {
        WashShopService service = new WashShopService();
        string url = "http://api.map.baidu.com/geocoding/v3/";

        public void GetLocation(string address)
        {
            string sk = "yQsHOCB0OLhfzf2lzTh6oiHsK170MG8R";
            string ak = "O1fv6rD1z4yx54M3t9hMROijUsGTaKfB";
          var list=  service.QueryAll();
            foreach (var item in list)
            {
                Dictionary<string, string> dics = new Dictionary<string, string>();
                dics.Add("address", item.Address);
                dics.Add("output", "json");
                dics.Add("ak", ak);
                string cityName = "北京市";
                if(item.CityID!=201)
                {
                    cityName = "三河市";
                }
                dics.Add("city", cityName);

                //dics.Add("city", "北京市");
                string sn = AKSNCaculater.CaculateAKSN(ak, sk, "/geocoding/v3/", dics);
                string tmp = url + string.Format("?address={0}&output=json&ak={1}&city={2}&sn=" + sn, item.Address, ak,cityName);
                // url = url + string.Format("?address={0}&output=json&ak=O1fv6rD1z4yx54M3t9hMROijUsGTaKfB&callback=showLocation", AKSNCaculater.UrlEncode(address));
                WebClient client = new WebClient();
                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                client.Encoding = Encoding.UTF8;
                string rt = client.DownloadString(tmp);
                ResultData rts = SimpleJson.DeserializeObject<ResultData>(rt);

                if (rts.status == 0)
                {
                    item.Latitude = Convert.ToDouble(rts.result.location.lat);
                    item.Longitude = Convert.ToDouble(rts.result.location.lng);
                    item.UpdateDate = DateTime.Now;
                    service.Update(item);
                }
            }
           
        }

        // {"status":0,"result":{"location":{"lng":116.4774573138944,"lat":39.91578946723788},"precise":1,"confidence":75,"comprehension":100,"level":"商务大厦"}}

    }

    public class ResultData
    {     
        /// <summary>
        /// 返回状态
        /// 0：成功,否则失败
        /// </summary>
        public int status { set; get; }

        /// <summary>
        /// 返回信息
        /// </summary>
        public RData result { set; get; }
    }

    public class RData
    {
        public Location location { set; get; }
        public int precise { set; get; }
        public int confidence { set; get; }
        public int comprehension { set; get; }
        public string level { set; get; }
    }

    public class Location
    {
      public decimal lng { set; get; }
        public decimal lat { set; get; }
    }
}
